import vue from '@vitejs/plugin-vue';
import {defineConfig} from 'vite';
import {resolve} from 'path';
import {readFileSync} from 'fs';
import {babel} from '@rollup/plugin-babel'
import compression from 'vite-plugin-compression';
import typescript from "@rollup/plugin-typescript";


const additionalData = readFileSync(resolve(__dirname, './src/cus-var.scss'), 'utf-8');

export default defineConfig({
    resolve: {
        alias: [
            {find: '@', replacement: resolve(__dirname, './src')},
            {find: '$', replacement: resolve(__dirname, './src/utils/$')}
        ]
    },
    build: {
        target: 'chrome90', // top-level-await chrome86以上版本支持。 指定esbuild Syntax transform target for version > chrome86
        rollupOptions: {
            inlineDynamicImports: true,
            plugins: [babel({
                babelHelpers: 'bundled',
                extensions: ['.js', '.ts', 'tsx'], // 再rollup中如果想要使用babel转换ts必须配置extensions：[...[.ts,.tsx]]
                presets: [
                    ['@babel/preset-typescript'],
                    [
                        "@babel/preset-env",
                        {
                            useBuiltIns: 'usage',
                            corejs: 3,
                        }
                    ],
                ],
                plugins: ["@babel/plugin-syntax-top-level-await"],
                exclude: 'node_modules/**'
            })] // 开启rollup转换ts代码 typescript()
        }
    },
    plugins: [vue(), compression()],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: "@import '@/cus-var.scss';"
            }
        }
    }
})
